$(function() {
	// 库存
	var stock = 0;
	// 计算总价
	function getSum() {
		var sum = Number($(".priceOne").html()) * Number($(".num-input").html());
		$(".much").eq(0).html(sum);
		// console.log(sum)
		// console.log(Number($(".priceOne").html()));
	}


	// 点击返回上一页
	$(document).on("click", ".titleTea a", function() {
		window.history.back();
	})

	// 获取
	// gid商品id
	var gid = window.sessionStorage.getItem("gid");
	$.ajax({
			url: 'http://www.teaweilai.com/app/Admin/AppGoodsInfo/goodsInfo',
			type: 'POST',
			dataType: 'json',
			data: {
				gid: gid
			},
			beforeSend: function() {
				console.log(gid);
			},
			success: function(data) {

				stock = data.sing.goods_number;

				// 判段库存为0时
				if (stock == 0) {
					$(".mask").css({
						"display": 'block',
						"height": "5rem"
					});
					$(".mask span").html("对不起，此商品售完");
					$(".pay").parents(".col-xs-3").css({
						"backgroundColor": "#ccc"
					});
					$(".pay").click(function() {
						return false;
					})
					$(".shop_cart").parents(".col-xs-4").css({
						"backgroundColor": "#ccc"
					});
					$(".shop_cart").click(function() {
						return false;
					})

				}
				console.log(data);
				var strGoods = template("goods_detail", {
					goodsD: data
				})
				$(".teadetail").html(strGoods);
				var str = data.sing.goods_desc;
				var arr = str.split('src="');
				var urlStr = data.sing.TEA_URL;
				var str2 = arr.join('src="' + urlStr);
				$(".tea_detailImg div").html(str2);
				// $(".tea_detailImg div").html(data.sing.goods_desc);
				getSum();
				// 点击改变商品数量
				$(document).on('click', ".count span", function() {
					console.log(stock);
					var num = $('.num-input').html();
					if ($(this).text() == '+') {
						$('.num-input').html(Number(num) + 1);
						console.log($('.num-input').html(Number(num) + 1))
							// 判段库存
						if (Number($('.num-input').html() + 1) > stock) {
							$(".mask").css({
								"display": 'block',
								"height": "5rem"
							});
							$(".mask span").html("对不起，此商品库存不够");
							$(".pay").parents(".col-xs-3").css({
								"backgroundColor": "#ccc"
							});
							$(".shop_cart").parents(".col-xs-4").css({
								"backgroundColor": "#ccc"
							});

						}
					} else if ($(this).text() == '-') {
						if (num == 1) {
							alert('不能再少了');
						} else {
							$('.num-input').html(Number(num) - 1)
						}
					}
					getSum();
				});

				// 加入购物车
				console.log($(".others h2").attr('data-goods_number'));
				var uid = window.localStorage.getItem("user_id");
				$(".shop_cart").click(function() {
						if ($(".others h2").attr('data-goods_number') == 0) {
							$(".shop_car").click(function() {
								return false;
							})
						} else {
							var goods_num = $(".num-input").html();
							var seller_id = $(".teadetail h2").attr('data-seller_id');
							$.ajax({
								url: 'http://www.teaweilai.com/app/Admin/AppCart/addCart',
								type: 'POST',
								dataType: 'json',
								data: {
									uid: uid,
									goods_id: gid,
									goods_num: goods_num,
									seller_id: seller_id
								},
								success: function(data) {
									console.log(data);
									$(".mask").css({
										display: "block"
									});
									setTimeout(function() {
										$(".mask").css({
											display: "none"
										});
									}, 2000)
								},
								beforeSend: function() {
									if (uid == null) {
										window.location.href = "login.html";
									}
								}
							})
						}
					})
					// 立即支付
				$(".pay").click(function() {
					if (uid == null) {
						window.location.href = "login.html";
					}else if($(".others h2").attr('data-goods_number') == 0){
						$(".pay").click(function(){
							return false;
						})
					} else {
						window.sessionStorage.setItem("gid", $("h2").eq(0).attr('data-goods_id'));
						window.sessionStorage.setItem("number", $(".num-input").html());
						var fromDetail = 1;
						window.sessionStorage.setItem("fromDetail", fromDetail);
						window.location.href = "order_detail.html";
						// console.log(window.sessionStorage.getItem("gid"))

					}
				})
			}
		})
		.fail(function() {
			$(".teadetail").html('<h2>不好意思您查看的物品找不到了。。。</h2>');
		})
		.always(function() {
			console.log("always");
		});



})